package com.invengo.module_check.ui.fragment.feature;

import static com.invengo.module_check.data.entity.FeatureBean.FIRST_MODULE;
import static com.invengo.module_check.data.entity.FeatureBean.LAYER_MODULE;
import static com.invengo.module_check.data.entity.FeatureBean.PUBLIC_MODULE;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.invengo.module_check.data.entity.FeatureBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.common.CommonRepsitory;
import com.invengo.library_rfid.entity.TagInfo;
import com.invengo.module_check.ui.fragment.feature.item.FeatureItemViewModel;

import java.util.ArrayList;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;
import me.tatarka.bindingcollectionadapter2.collections.MergeObservableList;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */
public class FeatureViewModel extends BaseViewModel<CommonRepsitory> {
    private SingleLiveEvent<ArrayList<FeatureBean>> data = new SingleLiveEvent<>();
    public BindingRecyclerViewAdapter adapter = new BindingRecyclerViewAdapter();
    public ObservableBoolean isLayermodule = new ObservableBoolean(ACache.getInstance().getAsBoolean(AKey.FEATURE_MODULE));

    public FeatureViewModel(@NonNull Application application) {
        super(application, CommonRepsitory.getInstance());
        data.setValue(model.getFeatureList());
        for (FeatureBean featureBean : data.getValue()) {
            switch (featureBean.getModel()) {
                case LAYER_MODULE:
                    layerList.add(new FeatureItemViewModel(this, featureBean));
                    break;
                case PUBLIC_MODULE:
                    publicList.add(new FeatureItemViewModel(this, featureBean));
                    break;
                case FIRST_MODULE:
                    firstList.add(new FeatureItemViewModel(this, featureBean));
                    break;
            }
        }
    }


    //给RecyclerView添加ObservableList
    public ObservableList<FeatureItemViewModel> publicList = new ObservableArrayList<>();
    public ObservableList<FeatureItemViewModel> layerList = new ObservableArrayList<>();
    public ObservableList<FeatureItemViewModel> firstList = new ObservableArrayList<>();

    public MergeObservableList<Object> list = new MergeObservableList<>()
            .insertItem(this)
            .insertItem("层位定位")
            .insertList(layerList)
            .insertItem("首书定位")
            .insertList(firstList)
            .insertItem("公共模块")
            .insertList(publicList);

    //给RecyclerView添加ItemBinding
    public ItemBinding<Object> itemBinding = ItemBinding.of((itemBinding, position, item) -> {
        if (getClass().equals(item.getClass())) {
            itemBinding.set(BR.module, R.layout.item_feature_module);
        } else if (String.class.equals(item.getClass())) {
            itemBinding.set(BR.titleName, R.layout.item_feature_title);
        } else if (FeatureItemViewModel.class.equals(item.getClass())) {
            itemBinding.set(BR.viewModel, R.layout.item_feature);
        }
    });


    /**
     * 保存功能
     */
    public void saveFeature() {
        ACache.getInstance().put(AKey.CACHE_FEATURE_LIST, data.getValue());
    }

    public void changeModule(boolean isChecked) {
        KLog.e("isch.....");
        ACache.getInstance().put(AKey.FEATURE_MODULE, isChecked);
        isLayermodule.set(isChecked);
        for (FeatureItemViewModel featureItemViewModel : layerList) {
            featureItemViewModel.changeModule(!isChecked);
        }
        for (FeatureItemViewModel featureItemViewModel : firstList) {
            featureItemViewModel.changeModule(isChecked);
        }
        adapter.notifyDataSetChanged();
    }
}
